package cn.zhangjie.org.service.impl;

import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.basic.util.BaiduAuditUtils;
import cn.zhangjie.org.domain.Employee;
import cn.zhangjie.org.domain.Shop;
import cn.zhangjie.org.domain.ShopLog;
import cn.zhangjie.org.dto.ShopDto;
import cn.zhangjie.org.mapper.EmployeeMapper;
import cn.zhangjie.org.mapper.ShopLogMapper;
import cn.zhangjie.org.mapper.ShopMapper;
import cn.zhangjie.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private ShopLogMapper logMapper;

    @Override
    public void settlement(Shop shop) {
        //实现shop入驻流程
        //1非空校验
        if(StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getEmployee().getUsername())
                || StringUtils.isEmpty(shop.getEmployee().getPhone())
                || StringUtils.isEmpty(shop.getEmployee().getEmail())
                || StringUtils.isEmpty(shop.getEmployee().getPassword())
                || StringUtils.isEmpty(shop.getEmployee().getComfirmPassword())

        ){
            throw new BusinssException("参数不能为空");
        }

        //通过百度ai审核name是否违规
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){
            throw new BusinssException("名称存在违规");
        }

        Employee employee = shop.getEmployee();
        //两次密码不一致
        if(!employee.getPassword().equals(employee.getComfirmPassword())){
            throw new BusinssException("两次密码不一致");

        }
        //3.店铺是否注册过校验
        Shop dbshop = shopMapper.getShopByMessage(shop);
        if(dbshop!=null){
            throw new BusinssException("该店铺已经被注册过了！");
        }
        //把shop和employee数据保存在表中
        //1.保存employee，获取自增主键
        employeeMapper.save(employee);
        //2把自增主键设置到shop表中的admin—id中
        shop.setAdmin_id(employee.getId());
        //3保存shop数据，返回自增主键
        shopMapper.save(shop);
        //4.返回自增主键设置给employee中shop-id字段
        employee.setShop_id(shop.getId());
        //5更新employee数据
        employeeMapper.update(employee);


    }

    @Override
    public void pass(ShopLog log) throws MessagingException {
        //修改shop的状态
        //通过shop-id查询shop数据，在修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核通过
        shop.setState(2);
        shopMapper.update(shop);
        //录入日志
        log.setState(2);
        log.setAudit_id(1l);
        logMapper.save(log);
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2406003812@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208/group1/M00/00/F5/CgAIC2LdNEaAI8xlAADrLDTa4y8189.jpg' ><a href='http://123.207.27.208/group1/M00/00/F5/CgAIC2LdNEaAI8xlAADrLDTa4y8189.jpg'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("aa.jpg",new File("D:\\壁纸\\aa.jpg"));

        //收件人
        helper.setTo("2406003812@qq.com");
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopLog log) throws MessagingException {
//修改shop的状态
        //通过shop-id查询shop数据，在修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核不通过
        shop.setState(4);
        shopMapper.update(shop);
        //录入日志
        log.setState(4);
        log.setAudit_id(1l);
        logMapper.save(log);
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2406003812@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><img src='http://123.207.27.208/group1/M00/00/F5/CgAIC2LdNEaAI8xlAADrLDTa4y8189.jpg' ><a href='http://123.207.27.208/group1/M00/00/F5/CgAIC2LdNEaAI8xlAADrLDTa4y8189.jpg'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("aa.jpg",new File("D:\\壁纸\\aa.jpg"));

        //收件人
        helper.setTo("2406003812@qq.com");
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }

}